#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(&window, SIGNAL(accepted()), this, SLOT(reset_table()));
    lock_size(ui->show);
    lock_size(ui->tabs);
    lock_pos(ui->just_do);
    lock_pos(ui->select);
    lock_size(ui->text);
    lock_pos(ui->commit);
    read_plain_text();
    table.write();
    window.set_selector(selector);
    emit window.accepted();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent* event)
{
    for (auto& r : lock_ui)
        r.first->resize(event->size()-r.second);
    for (auto& r : locat_ui)
        r.first->move(r.first->x(), event->size().height()-r.second);
}

void MainWindow::lock_size(QWidget* widget)
{
    QSize sz {size()-widget->size()};
    lock_ui.emplaceBack(widget, sz);
}

void MainWindow::lock_pos(QWidget* widget)
{
    locat_ui.emplaceBack(widget, height()-widget->y());
}

void MainWindow::paste(double scale)
{
    auto* docum = ui->show->document();
    QPixmap snap {(docum->size()*scale).toSize()};
    QPainter pa {&snap};
    pa.scale(scale, scale);
    docum->drawContents(&pa);
    pa.end();
    snap.save(file_save);
    ui->statusbar->showMessage(tr("图片已保存在")+file_save);
}

void MainWindow::read_plain_text()
{
    ui->text->setPlainText(table.text());
    ui->text->document()->setModified(false);
}

void MainWindow::commit()
try
{
    table.read(ui->text->toPlainText());
    read_plain_text();
    table.write();
    ui->show->setText(table.select(selector).html());
    ui->statusbar->showMessage(tr("已更新文件 ")+table.file_name());
}
catch (Code::No_start_error) {
    QMessageBox::warning(this, tr("代码格式错误"), tr("缺失开始标记start"));
    throw;
}
catch (Code::No_end_error) {
    QMessageBox::warning(this, tr("代码格式错误"), tr("缺失结束标记end"));
    throw;
}
catch (const Chess::Format_error& err) {
    QMessageBox::warning(this, tr("代码格式错误"), tr("%1\n\n位置：\n%2").arg(err.message).arg(err.code));
    throw;
}

void MainWindow::on_just_do_clicked()
{
    paste();
}

void MainWindow::on_commit_clicked()
try
{
    commit();
}
catch (...) {
    // 什么也不做
}

void MainWindow::reset_table()
{
    ui->tabs->setCurrentIndex(0);
    selector = window.mov_selector();
    ui->show->setText(table.select(selector).html());
}

void MainWindow::on_select_clicked()
{
    window.set_selector(selector);
    window.show();
}

void MainWindow::on_tabs_currentChanged(int index)
try
{
    if (index == 1)
        return;
    if (ui->text->document()->isModified()) {
        ui->tabs->setCurrentIndex(1);
        auto choice = QMessageBox::question(this, tr("未保存的更改"), tr("文本未保存，是否保存更改？"), QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
        switch (choice) {
        case QMessageBox::Yes:
            commit();
            break;
        case QMessageBox::No:
            read_plain_text();
            break;
        case QMessageBox::Cancel:
            return;
        }
        ui->tabs->setCurrentIndex(index);
    }
}
catch (...) {
    // 什么也不做
}
